//
//  MySpeViewController.m
//  Project_B
//
//  Created by 黑黑 on 15/11/28.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "MySpeViewController.h"
#import "SpecialModel.h"
#import "WCG_O_SectonHeaderTableViewCell.h"
#import "WCG_O_DetailViewController.h"
@interface MySpeViewController () <UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation MySpeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的专题";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.dataArr = [NSMutableArray array];
    [self getData];
    [self createTableViewWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    [self.tableView registerClass:[WCG_O_SectonHeaderTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:self action:@selector(left)];
    [UIView animateWithDuration:1.5 animations:^{
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
        [self.view addSubview:button];
        [button setBackgroundImage:[UIImage imageNamed:@"bottom_btn_back_active"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backToFront) forControlEvents:UIControlEventTouchUpInside];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.view).with.offset(15);
            make.bottom.mas_equalTo(self.view).with.offset(-15);
            make.width.mas_equalTo(35);
            make.height.mas_equalTo(35);
        }];
    }];
}

- (void)left
{
    NSLog(@"你看不到我~");
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIView *temp = self.tabBarController.view.subviews[2];
    [UIView animateWithDuration:1 animations:^{
        self.navigationController.navigationBar.translucent = NO;
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.navigationController.navigationBarHidden = NO;
        temp.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 49);
    }];
}

- (void)getData
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SpecialModel" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
#if 1
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type = %@", @"special"];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
#endif
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"空");
    }
    for (SpecialModel *spe in fetchedObjects) {
        [self.dataArr addObject:spe];
    }
    [self.tableView reloadData];
}
#pragma mark - tableView协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCG_O_SectonHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.titleLable.text = [self.dataArr[indexPath.row] title];
    NSString *temp = [NSString stringWithFormat:@"%@人订阅 | %@篇", [self.dataArr[indexPath.row] subscriber_num], [self.dataArr[indexPath.row] content_num]];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:temp];
    NSRange subRange = [temp rangeOfString:[[self.dataArr[indexPath.row] subscriber_num] stringValue]];
    [string addAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor]} range:subRange];
    NSMutableAttributedString *finalString = [[NSMutableAttributedString alloc] initWithAttributedString:string];
    
    NSRange conRange = [temp rangeOfString:[[self.dataArr[indexPath.row] content_num] stringValue]];
    [finalString addAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor]} range:conRange];
    
    cell.subtitleLable.attributedText = finalString;
    
    [cell.mainImage sd_setImageWithURL:[NSURL URLWithString:[self.dataArr[indexPath.row] title_font]]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WCG_O_DetailViewController *detailVC = [[WCG_O_DetailViewController alloc] init];
    detailVC.specialID = [self.dataArr[indexPath.row] special_id];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - 自定义按钮返回方法
- (void)backToFront
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 当数据为空时显示的图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    return [UIImage imageNamed:@"icon-search-article"];
}

#pragma mark - 当数据为空时显示的字符
-(NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"还没有订阅专题哦~";
    NSDictionary *str = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f]};
    return [[NSAttributedString alloc] initWithString:text attributes:str];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
